package fr.univ.lr.icone.agents;

/**
 * 
 * @author Nyko
 * 
 */

public class Agent implements IAgent {

	/* *********************************************************** */
	/*       *** Variables ********************************************* */
	/* *********************************************************** */

	private int id;
	private int x, y;
	private int opinion;
	private int confiance;
	private int step;
	private int clock;
	private int clockmax;

	/* *********************************************************** */
	/*       *** Constructors ****************************************** */
	/* *********************************************************** */

	public Agent(int ident) {
		this.id = ident;
		this.confiance = 1 + (int) (Math.random() * 4); // Compris entre 1 et 5
		this.opinion = (int) (Math.random() * 10); // Compris entre 0 et 9
		this.x = 0;
		this.y = 0;
		this.step = 1;
		this.clock = 0;
		this.clockmax = 3;

		/* System.out.println("Constructeur OK (1/1)"); */
	}

	public Agent(int ident, int st, int x, int y) {
		this(ident);
		this.x = x;
		this.y = y;
		this.step = st;

	}

	public Agent(int ident, int op, int cf, int x, int y) throws Exception {
		this(ident, op, cf, x, y, 1, 0);
	}

	public Agent(int ident, int op, int cf, int x, int y, int pas, int cl)
			throws Exception {

		this.id = ident; // Identifiant de l'agent

		// Opinion is between 0 and 9 ?
		// YES : Affect the value
		// NON : Error report
		if ((op < 0) || (op > 9)) {
			throw new Exception("Erreur dans la valeur opinion");
		} else {
			this.opinion = op;
		}
		// Confiance est entre 1 et 5 ?
		// OUI : Affectation de la valeur
		// NON : Rapport d'erreur
		if ((cf < 1) || (cf > 5)) {
			throw new Exception("Erreur dans la valeur confiance");
		} else {
			this.confiance = cf;
		}
		this.x = x; // Position en X
		this.y = y; // Position en Y

		// Le pas = 1 ou 2 ?
		// OUI : Affectation de la valeur
		// NON : Rapport d'erreur
		if ((pas != 1) && (pas != 2)) {
			throw new Exception("Erreur dans la valeur step");
		} else {
			this.step = pas;
		}

		this.clock = cl;
		/* System.out.println("Constructeur OK (2/2)"); */
	}

	/* *********************************************************** */
	/*       *** Get *************************************************** */
	/* *********************************************************** */

	/**
	 * 
	 * @return Agent position X
	 */
	public int getX() {
		return this.x;
	}

	/**
	 * 
	 * @return Agent position Y
	 */
	public int getY() {
		return this.y;
	}

	/**
	 * 
	 * @return Agent opinion
	 */
	public int getOpinion() {
		return this.opinion;
	}

	/**
	 * 
	 * @return Agent Confiance
	 */
	public int getConfiance() {
		return this.confiance;
	}

	/**
	 * 
	 * @return Agent identification
	 */
	public int getId() {
		return this.id;
	}

	public int getStep() {
		return this.step;
	}

	public int getClock() {
		return this.clock;
	}

	public int getClockmax() {
		return this.clockmax;
	}

	/* *********************************************************** */
	/*       *** Set *************************************************** */
	/* *********************************************************** */

	/**
	 * 
	 * @param x
	 */
	public void setX(int x) {
		this.x = x;
	}

	/**
	 * 
	 * @param y
	 */
	public void setY(int y) {
		this.y = y;
	}

	/**
	 * 
	 * @param newOpinion
	 */
	public void setOpinion(int newOpinion) {
		this.opinion = newOpinion;
	}

	/**
	 * 
	 * @param newConfiance
	 */
	public void setConfiance(int newConfiance) {
		this.confiance = newConfiance;
	}

	/**
	 * 
	 * @param newClockmax
	 */
	public void setClockmax(int newClockmax) {
		this.clockmax = newClockmax;
	}

	/* *********************************************************** */
	/*       *** Set *************************************************** */
	/* *********************************************************** */

	public void tic() {
		this.clock++;
		if (clock == this.clockmax) {
			clock = 0;
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + clock;
		result = prime * result + clockmax;
		result = prime * result + confiance;
		result = prime * result + id;
		result = prime * result + opinion;
		result = prime * result + step;
		result = prime * result + x;
		result = prime * result + y;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof Agent)) {
			return false;
		}
		Agent other = (Agent) obj;
		if (confiance != other.confiance)
			return false;
		if (id != other.id)
			return false;
		if (opinion != other.opinion)
			return false;
		if (x != other.x)
			return false;
		if (y != other.y)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Agent [clock=" + clock + ", clockmax=" + clockmax
				+ ", confiance=" + confiance + ", id=" + id + ", opinion="
				+ opinion + ", step=" + step + ", x=" + x + ", y=" + y + "]";
	}

}